﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JewererBasic.Models;
using JewererBasic.BO;
using JewererBasic.Classes;

namespace JewererBasic.Controllers
{
    public class MouldController : Controller
    {
        private MSQDBEntities db = new MSQDBEntities();

        private List<TYPE> listTypes;
        private List<UNIT> listUnits;

        #region Index Home
        public ActionResult Index()
        {
            ImportedMoulds.Clear();
            ExportMoulds.Clear();

            listTypes = new List<TYPE>(db.TYPEs.ToList());
            listUnits = new List<UNIT>(db.UNITs.ToList());

            ViewBag.Types = listTypes;
            ViewBag.Units = listUnits;

            return View(db.MOULDs.ToList());
        }

        [HttpPost]
        public ActionResult Index(FormCollection fc)
        {
            ImportedMoulds.Clear();
            ExportMoulds.Clear();

            listTypes = new List<TYPE>(db.TYPEs.ToList());
            listUnits = new List<UNIT>(db.UNITs.ToList());

            ViewBag.Types = listTypes;
            ViewBag.Units = listUnits;

            string id = fc["txtId"];
            string name = fc["txtName"];
            string quantity = fc["txtQuantity"];

            if (fc["submit"] == "ADD")
            {
                if (String.IsNullOrEmpty(id) || String.IsNullOrEmpty(name))
                {
                    return View(db.MOULDs.ToList());
                }
                else
                {
                    try
                    {
                        MOULD Mould = new MOULD();
                        Mould.IdMould = id;
                        Mould.Name = name;
                        Mould.Numb = Int32.Parse(quantity);

                        MouldBO.Add(Mould);
                    }
                    catch (Exception e)
                    {
                        return View(db.MOULDs.ToList());
                    }
                }
            }
            else if (fc["submit"] == "SEARCH")
            {
                string searchId = fc["txtSearchId"];
                string searchName = fc["txtSearchName"];
                return View(MouldBO.Search(searchId, searchName));
            }
            else if (fc["submit"] == "IMPORT")
            {
                return RedirectToAction("Import");
            }
            else if (fc["submit"] == "EXPORT")
            {
                return RedirectToAction("Export");
            }

            return View(db.MOULDs.ToList());
        }

        public ActionResult AddMould(string id, string name, int? quantity)
        {
            bool result = true;

            MOULD s = new MOULD();
            s.IdMould = id;
            s.Name = name;
            s.Numb = quantity;

            result = MouldBO.Add(s);

            return Json(new { result }, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region Import
        private static List<MOULD> ImportedMoulds = new List<MOULD>();
        public ActionResult Import()
        {
            return View(ImportedMoulds);
        }

        [HttpPost]
        public ActionResult Import(FormCollection fc)
        {
            string id = fc["txtId"];
            string quantity = fc["txtQuantity"];

            MouldBO.Import(ImportedMoulds);

            ImportedMoulds.Clear();

            return RedirectToAction("Index");
        }
        #endregion

        #region Export
        private static List<MOULD> ExportMoulds = new List<MOULD>();
        public ActionResult Export()
        {
            return View(ExportMoulds);
        }

        [HttpPost]
        public ActionResult Export(FormCollection fc)
        {
            string id = fc["txtId"];
            string quantity = fc["txtQuantity"];

            MouldBO.Export(ExportMoulds);

            ExportMoulds.Clear();

            return RedirectToAction("Index");
        }
        #endregion

        #region Json
        public ActionResult SaveToDb(List<MouldViewModel> Moulds)
        {
            bool result = true;

            foreach (var item in Moulds)
            {
                MOULD Mould = new MOULD();
                Mould.IdMould = item.Id;
                Mould.Name = item.Name;
                Mould.Numb = item.Quantity;

                MouldBO.Add(Mould);
            }

            return Json(new { result }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult ClearImportForm()
        {
            bool result = true;

            ImportedMoulds.Clear();

            return Json(new { result }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult ClearExportForm()
        {
            bool result = true;

            ExportMoulds.Clear();

            return Json(new { result }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult IsMouldExisted(string id)
        {
            bool result;

            MOULD p = db.MOULDs.Find(id);
            if (p == null)
            {
                result = false;
            }
            else
            {
                result = true;
            }

            return Json(new { result }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AddImport(string id, int quantity)
        {
            bool result = true;

            ImportedMoulds.Add(new MOULD() { IdMould = id, Numb = quantity });

            return Json(new { result }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AddExport(string id, int quantity)
        {
            bool result = true;

            ExportMoulds.Add(new MOULD() { IdMould = id, Numb = quantity });

            return Json(new { result }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetMoulds(string query = null)
        {
            var result = from d in db.MOULDs
                         where d.IdMould.Contains(query)
                         select d.IdMould;
            var s = result.ToList();
            return Json(result.ToList(), JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetMouldNames(string query = null)
        {
            var result = from d in db.MOULDs
                         where d.Name.Contains(query)
                         select d.Name;

            return Json(result.Distinct().ToList(), JsonRequestBehavior.AllowGet);
        }

        public JsonResult Update(string id, string name)
        {
            bool result = true;

            MOULD s = new MOULD() { IdMould = id, Name = name };
            MouldBO.Update(s);

            return Json(new { result }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult UpdateImportMould(string id, int quantity)
        {
            bool result = true;

            MOULD s = ImportedMoulds.Where(m => m.IdMould == id).FirstOrDefault();
            if (s == null)
                result = false;
            else
            {
                s.Numb = quantity;
            }

            return Json(new { result }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DeleteImportMould(string id)
        {
            bool result = true;

            MOULD s = ImportedMoulds.Where(m => m.IdMould == id).FirstOrDefault();
            if (s == null)
                result = false;
            else
                ImportedMoulds.Remove(s);

            return Json(new { result }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult UpdateExportMould(string id, int quantity)
        {
            bool result = true;

            MOULD s = ExportMoulds.Where(m => m.IdMould == id).FirstOrDefault();
            if (s == null)
                result = false;
            else
            {
                s.Numb = quantity;
            }

            return Json(new { result }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DeleteExportMould(string id)
        {
            bool result = true;

            MOULD s = ExportMoulds.Where(m => m.IdMould == id).FirstOrDefault();
            if (s == null)
                result = false;
            else
                ImportedMoulds.Remove(s);

            return Json(new { result }, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}